package simonxianyu.demo.shiro.util.shiro;

import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.Objects;

/**
 *
 * Created by simon on 2017/3/4.
 */
public class MyFormAuthFilter extends FormAuthenticationFilter {
  @Autowired
  private JdbcTemplate jdbcTemplate;
  @Override
  protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
    String loginId = Objects.toString(token.getPrincipal(), "");
    String nickname = jdbcTemplate.queryForObject("select nickname from sysuser where loginid=?", new Object[]{loginId}, String.class);
    subject.getSession().setAttribute("nickname", nickname);
    return super.onLoginSuccess(token, subject, request, response);
  }
}
